﻿using Management.Systeam.Domaina;
using Management.Systeam.ErrorCode;
using Management.Systeam.Infrastructure.Implement;
using Management.Systeam.Infrastructure.Interface;
using Management.Systeam.Read.Apis.Application.Command.InventoryAudits;
using MediatR;
using System.Text;

namespace Management.Systeam.Read.Apis.Application.CommandHandler.InventoryAudits
{
    public class FirstInventoryAuditsCommandHandler : IRequestHandler<FirstInventoryAuditsCommand, ApiResult<InventoryAudit>>
    {
        IBaseRepository<InventoryAudit> InventoryAudit;
        public DatabaseContext db;
        
        public FirstInventoryAuditsCommandHandler(IBaseRepository<InventoryAudit> InventoryAudit, DatabaseContext db)
        {
            this.InventoryAudit = InventoryAudit;
            this.db = db;
        }

        public async Task<ApiResult<InventoryAudit>> Handle(FirstInventoryAuditsCommand request, CancellationToken cancellationToken)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT * FROM InventoryAudit");
            sb.Append(" WHERE IsDel LIKE '%" + 0 + "%'");

                sb.Append(" AND InventoryAudit_Id = '" + request.id + "'");
        
            sb.Append(" ORDER BY InventoryAudit_Id DESC");
            var list = await InventoryAudit.QueryAsync(sb.ToString());

            return new ApiResult<InventoryAudit>
            {
                Code = ApiEnum.success,
                Message = "获取用户列表成功",
                Data = list.FirstOrDefault()
            };
        }
    }
}
